#include "graphsupport/solve/facilitylocation/PrimalDualFacilitiesLocationSolver.h"

#include "accumulsupport/AccumulSupport.h"

#include "graphsupport/solve/facilitylocation/PrimalDualSolution.h"

bool PrimalDualFacilitiesLocationSolver::solve(
	FacilitiesGraph & fgraph,
	std::vector< char > & isFacilityOpenVec,
	std::vector< int > & cityWitnessVec
)
{
	try {
		int citiesCount = fgraph.getCitiesCount();
		int facilitiesCount = fgraph.getFacilitiesCount();

		PrimDualSolvContext scontext( citiesCount, facilitiesCount );
		scontext.fgraph = fgraph;
		PrimalDualSolution::createWitnessForAllCities( scontext );
		PrimalDualSolution::computeSpecialEdgesSquareSubgraph( scontext );
		PrimalDualSolution::computeMaximalIndependentSet( scontext );
		PrimalDualSolution::computeFacilityAssociations( scontext );
		PrimalDualSolution::openCheapestForNotExist( scontext );

		cityWitnessVec = scontext.associatedFacility;
//		for ( int i = 0; i < citiesCount; i++ )
//		{
//			cityWitnessVec[i] += citiesCount;// TODO: remove this
//		}
		isFacilityOpenVec = scontext.indepFacilities;
	} catch ( SolutionProcessException & e )
	{
		return false;
	}
	return true;
}
